package com.owen.he.jee.service.subService;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.owen.he.jee.model.sms.SmsProvider;
import com.owen.he.jee.service.SubService;
import com.owen.he.jee.utils.BizNumberUtil;
import com.owen.he.jee.utils.MD5;
import com.owen.he.jee.vo.ReturnData;

/**
 * 短信发送bean
 */
@Component
public class SmsSender implements SubService{
	
	private static Log logger = LogFactory.getLog(SmsSender.class);
	
	public ReturnData sendSms(String mobies,String content, SmsProvider provider) {
		ReturnData data = null;
		String pro = provider.getProvider();
		if("gysoft".equals(pro)) {
			data = sendSmsGysoft(mobies, content, provider);
		}else if("lx198".equals(pro)) {
			data = sendLx198(mobies, content, provider);
		}
		return data;
	}
	
	
	public ReturnData sendLx198(String mobies,String content, SmsProvider provider){
		logger.info("乐信平台发送短信.....");
		StringBuffer sb = new StringBuffer(provider.getUrl()+"?");
		String result = null;
		try {
			sb.append("&accName="+provider.getUserName()); 
			sb.append("&accPwd="+MD5.getMd5String(provider.getPassWord())); 
			sb.append("&aimcodes="+mobies);
			sb.append("&content="+URLEncoder.encode(content+provider.getSmsTag(),"UTF-8"));
			sb.append("&bizId="+BizNumberUtil.createBizId());
			sb.append("&dataType=string");
			URL url = new URL(sb.toString());
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			result =  in.readLine();
			logger.info("乐信平台发送短信返回信息："+result);
			if (result.indexOf("发送成功")>0) {
				return ReturnData.returnSuccess("乐信发送短信成功:" + result);
			} else {
				return ReturnData.returnFailure("乐信发送短信失败:" + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ReturnData.returnFailure("乐信发送短信失败:" + result);
		}
	}
	
	public ReturnData sendSmsGysoft(String mobile, String content, SmsProvider provider) {
		String result = null;
		try {
			logger.info("国宇平台发送短信.....");
			// 创建StringBuffer对象用来操作字符串
		    //http://www.gysoft.cn/smspost 如果你的网站是非utf8的
			StringBuffer sb = new StringBuffer(provider.getUrl()+"?");
		   //如果是发送验证，请用sendone.aspx网址
		    //StringBuffer sb = new StringBuffer("http://www.gysoft.cn/smspost_utf8/sendone.aspx?");
			// 向StringBuffer追加用户名和密码
			sb.append("username="+provider.getUserName());
			sb.append("&password="+provider.getPassWord());
			// 向StringBuffer追加手机号码，多个号码用,分隔
		            // 如果发送验证码，一次只能发送一个手机号
			sb.append("&mobile="+mobile);
			// 向StringBuffer追加消息内容转URL标准码
			//content = "测试短信流程"+DateUtils.date2String(new Date(),MonthUtils.YYYY_MM_DD_HH_MM_SS);
			sb.append("&content="+URLEncoder.encode(content+provider.getSmsTag()));
			// 创建url对象
			URL url = new URL(sb.toString());
			// 打开url连接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		
			// 设置url请求方式  post
			connection.setRequestMethod("POST");
			// 发送
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			// 返回发送结果
			// 返回结果为‘OK1’表示 发送成功 如果为Ok2则发送成功2条，OK3则发送3条
			result = in.readLine();
			logger.info("国宇平台发送短信返回信息："+result);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ReturnData.returnFailure(e.getMessage());
		}
		if (StringUtils.startsWith(result, "OK")) {
			return ReturnData.returnSuccess("国宇发送短信成功:" + result);
		} else {
			return ReturnData.returnFailure("国宇发送短信失败:" + result);
		}
	}
	
	public ReturnData sendYixin(String mobile, String content, SmsProvider provider) {
		String result = null;
		try {
			// 创建StringBuffer对象用来操作字符串
		    //http://www.gysoft.cn/smspost 如果你的网站是非utf8的
			StringBuffer sb = new StringBuffer(provider.getUrl()+"?");
		   //如果是发送验证，请用sendone.aspx网址
		    //StringBuffer sb = new StringBuffer("http://www.gysoft.cn/smspost_utf8/sendone.aspx?");
			// 向StringBuffer追加用户名和密码
			sb.append("zh="+provider.getUserName());
			sb.append("&mm="+provider.getPassWord());
			sb.append("&dxlbid=7");
			sb.append("&extno=7");
			// 向StringBuffer追加手机号码，多个号码用,分隔
		            // 如果发送验证码，一次只能发送一个手机号
			sb.append("&hm="+mobile);
			// 向StringBuffer追加消息内容转URL标准码
			//content = "测试短信流程"+DateUtils.date2String(new Date(),MonthUtils.YYYY_MM_DD_HH_MM_SS);
			sb.append("&nr="+URLEncoder.encode(content+provider.getSmsTag()));
			// 创建url对象
			URL url = new URL(sb.toString());
			// 打开url连接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		
			// 设置url请求方式  post
			connection.setRequestMethod("POST");
			// 发送
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			// 返回发送结果
			// 返回结果为‘OK1’表示 发送成功 如果为Ok2则发送成功2条，OK3则发送3条
			result = in.readLine();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ReturnData.returnFailure(e.getMessage());
		}
		return ReturnData.returnSuccess("益华发送短信成功:" + result);
	}
	
	
	

}
